import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;

import UtilFuncs from '../common/tools/utilfuncs.js'
var utilfuncs = UtilFuncs.UtilFuncs;

import conductPkg from './conduct.js'
import connlinePkg from './connline.js'
import connectnode from './connectnode.js';
import {KeyGenerator}  from '../common/keygenerator.js'
import { LGXEnums } from '../common/sysenum.js';

class TopoManager {
    constructor() {
        this.m_topoResList = new Array();
        this.m_tCurentNewResList = new Array();
    }

    productNewRes4NewGraph(pNewGraph) {
        if (!pNewGraph) {
            return -1;
        }
        var newTopoRes = this.createNewRes(pNewGraph);
        if (newTopoRes) {
            newTopoRes.setNewAdd(1);
            this.m_topoResList.push(newTopoRes);
            this.m_tCurentNewResList.push(newTopoRes);
        }
    }

    createNewRes(pNewGraph) {
        var newTopoRes = null;
        if (pNewGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
            newTopoRes = new connlinePkg.Connline();
        }
        else {
            newTopoRes = new conductPkg.Conduct();
        }
        newTopoRes.id = KeyGenerator.rid();
        newTopoRes.setRefGraph(pNewGraph);
        pNewGraph.setTopoRes(newTopoRes);
        return newTopoRes
    }

    createNewNode() {
        let connNode = new connectnode.ConnectNode();
        connNode.id = KeyGenerator.rid();
        connNode.setNewAdd(1);
        this.m_topoResList.push(connNode);
        this.m_tCurentNewResList.push(connNode);
        return connNode;
    }

    appendTopo2List(totalLst) {
        for (var idx in this.m_topoResList) {
            var topores = this.m_topoResList[idx];
            totalLst.push(topores);
        }
    }

    getRelateTopo(pRes, tRelateResOut, tRelateGphOut) {
        if (pRes) {
            this.add2Lst(pRes, tRelateResOut);
        }
    }

    add2Lst(data, list) {
        if (data && list.indexOf(data) == -1) {
            list.push(data);
        }
    }

    findResource(rid) {
        var topores;
        for (var idx in this.m_topoResList) {
            topores = this.m_topoResList[idx];
            if (topores.id == rid) {
                break;
            }
            topores = null;
        }
        return topores;
    }

    saveData(){
        let conductArr = this.saveTopoData("conduct");
        let connlineArr = this.saveTopoData("connline");
        let nodeArr = this.saveTopoData("node");
        let saveRet = {conductArr,connlineArr,nodeArr};
        return saveRet;
    }

    saveTopoData(type){
        let arr = [];
        for (var idx in this.m_topoResList) {
            let topores = this.m_topoResList[idx];
            if (topores.getUseful() != 0 && topores.type == type) {
                let info = topores.toInfo();
                if (topores.getUseful() != 0){
                    arr.push(info);
                }
            }
        }
        return arr;
    }

    initConduct = function(arr){
        let dataList = [];
        this.m_topoResList = [];
        for (var idx in arr) {
            let info = arr[idx];
            let data = new conductPkg.Conduct();
            data.id = info.id;
            this.m_topoResList.push(data);
            dataList.push(data);
        }
        return dataList;
    }

    initConnline = function(arr){
        let dataList = [];
        for (var idx in arr) {
            let info = arr[idx];
            let data = new connlinePkg.Connline();
            data.id = info.id;
            this.m_topoResList.push(data);
            dataList.push(data);
        }
        return dataList;
    }

    initNode = function(arr){
        let dataList = [];
        for (var idx in arr) {
            let info = arr[idx];
            let data = new connectnode.ConnectNode();
            data.id = info.id;
            data.x = info.x;
            data.y = info.y;
            this.m_topoResList.push(data);
            dataList.push(data);
        }
        return dataList;
    }

    clearData() {
        this.m_topoResList.length = 0;
    }


}
export default { TopoManager }